function Meteorit( width, height )
{      
    this.width = 10;
    this.height = 150;
    this.x = getXorY( width, this.height );
    this.y = getXorY( height, this.height );
    this.x_1 = getXorY( width, this.height );
    this.y_1 = getXorY( height, this.height );
    this.speed = 9;
    this.dx = getDxOrDy( this.x, this.x_1, this.y, this.y_1, this.speed );
    this.dy = getDxOrDy( this.y, this.y_1, this.x, this.x_1, this.speed );
    this.rotation = Math.atan2( this.dy, this.dx ) - Math.PI / 2;
    this.image = document.getElementById( 'meteorit' );
    this.life = 'false';
    this.color = "#ffffff";
}

function getDxOrDy( x, x_1, y, y_1, speed )
{
    var dx = ( x_1 - x );
    var dy = ( y_1 - y );
    var sp = Math.sqrt( dx*dx + dy*dy );
        
    dx = dx * speed / sp;
    return dx;
}

Meteorit.prototype.draw = function( context )
{
    context.save();

    context.shadowBlur = 50;
    context.shadowOffsetX = 0;
    context.shadowOffsetY = 0;
    context.shadowColor = utils.colorToRGB( this.color, 1 );
    context.translate( this.x, this.y );
    context.rotate( this.rotation );
    context.drawImage( this.image, 0, 0, this.width, this.height );
    
    context.restore();
};

function getMeteorits( numMeteorits, meteorits, width, height )
{
    var i = 0;
                            
    if ( meteorits.length == 0 )
    {
        for ( i = 0; i < numMeteorits; i++ )
        {
            meteorits[i] = new Meteorit( width, height );                                  
        }
    }
    else
    {
        for ( i = 0; i < numMeteorits; i++ )
        {   
            if ( meteorits[i].life == 'false' )
            {
                var percent = 0;
    
                percent = getRandomArbitary( 1, 100 );
                if ( percent <= 50 )
                {                
                    meteorits[i] = new Meteorit( width, height );
                    meteorits[i].life = 'true';                                                           
                }
            }
            else
            {            
                meteorits[i].x -= meteorits[i].dx;                                                 
                meteorits[i].y -= meteorits[i].dy;
                if ( meteorits[i].x > 2 * width || meteorits[i].x < -2 * width )
                {
                    meteorits[i].life = 'false';    
                }                                                                                      
                if ( meteorits[i].y > 2 * height || meteorits[i].y < -2 * height )
                {
                    meteorits[i].life = 'false';    
                }
            }
        }
    }
    return meteorits;
}

function getXorY( widthPole, meteoritLength )
{
    var x = Math.round( getRandomArbitary( 0, meteoritLength ) );
    var choice = Math.round( getRandomArbitary( 0, 1 ) );
    
    x = ( choice == 0 ) ? ( -x - meteoritLength ) : x + widthPole + meteoritLength;
    return Math.round( x );
}